package com.vincent.sciinfo.service.impl;


import com.vincent.sciinfo.mapper.ProjectMapper;
import com.vincent.sciinfo.pojo.Project;
import com.vincent.sciinfo.pojo.ProjectExample;
import com.vincent.sciinfo.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询指定教师，指定状态下的项目的信息。
     *
     * @param ope       项目状态
     * @param teacherId 教师Id
     * @return
     */
    public List<Project> selectProject(int ope, String teacherId) {
        ProjectExample projectExample = new ProjectExample();
        ProjectExample.Criteria circle = projectExample.createCriteria();
        circle.andTeacheridEqualTo(teacherId);
        circle.andCheckflagEqualTo(ope);
        return projectMapper.selectByExample(projectExample);
    }

    //根据项目类型 自动评分
    @Transactional
    @Override
    public int insertProject(Project project) {
        int codeInsert = projectMapper.insert(project);
        int codeScore = projectMapper.updateProjectScoreByType(project.getResultuuid());
        return codeInsert + codeScore;
    }

    @Override
    public Project selectOneProject(Project project) {
        ProjectExample projectExample = new ProjectExample();
        ProjectExample.Criteria criteria = projectExample.createCriteria();
        criteria.andProjectnameEqualTo(project.getProjectname());
        List<Project> projectList = projectMapper.selectByExample(projectExample);
        return (projectList.size() == 0) ? null : projectList.get(0);
    }

    /**
     * 查找所有通过审核或者被下架的项目
     */
    @Override
    public List<Project> selectCheckAndRemoveProject() {
        ProjectExample projectExample = new ProjectExample();
        ProjectExample.Criteria circle = projectExample.createCriteria();
        List<Integer> vals = new LinkedList<>();
        vals.add(0);
        vals.add(1);
        vals.add(3);
        circle.andCheckflagIn(vals);
        return projectMapper.selectByExample(projectExample);
    }

    @Override
    public void updateProjectStatus(String projectId, int operaCode) {
        Project project = projectMapper.selectByPrimaryKey(projectId);
        project.setCheckflag(operaCode);
        projectMapper.updateByPrimaryKey(project);
    }

    @Override
    public String[] selectProjectNameById(String projectId) {
        String[] strings = new String[2];
        Project project = projectMapper.selectByPrimaryKey(projectId);
        strings[0] = project.getTeacherid();
        strings[1] = project.getProjectname();
        return strings;
    }

}

